const Router = require('koa-router');
const tool = require("../../tools/tool");
const router = new Router();
const {db, Op, QueryTypes} = require("../../models/db");

// 点击单元格显示数据明细
router.post('/list/showDetail', async ctx => {
    let user = ctx.user
    let body = ctx.request.body
    let tenantId = user.tenantId;
    let page = body.page;
    let field = body.field ? body.field : '';
    let buId = body.buId ? body.buId : 0;
    let subjectId = body.subjectId ? body.subjectId : 0;
    let id = body.id ? body.id : 0;
    let locationId = body.locationId ? body.locationId : 0;
    let beginDate = body.beginDate ? body.beginDate : '';
    let endDate = body.endDate ? body.endDate : '';
    let billDate = body.billDate ? body.billDate : '';
    let buName = body.buName ? body.buName : '';
    let invId = body.invId ? body.invId : 0;
    let columns = '';
    let ret = {
        columns: [],
        list: []
    }

    try {
        if (page === 'customerbills') {
            if (!billDate) {
                return ctx.err('未选择时间')
            }
            beginDate = billDate.substring(0, 10)
            endDate = billDate.substring(11, 21)
            if (['ysAmount', 'ysPayed', 'yfAmount', 'yfPayed', 'ysUnPay'].indexOf(field) !== -1) {
                columns = [
                    {
                        field: 'seq',
                        type: 'seq',
                        title: '#',
                        width: 60,
                        align: 'center',
                        visible: true,
                        fixed: 'left'
                    },
                    {
                        field: 'checked',
                        type: 'checkbox',
                        title: '',
                        width: 40,
                        fixed: 'left',
                        visible: true,
                        align: 'center'
                    },
                    {
                        field: 'buName',
                        title: '往来单位',
                        width: 150
                    },
                    {
                        field: 'billDate',
                        title: '票据日期',
                        width: 100
                    },
                    {
                        field: 'srcBillNo',
                        title: '原票号',
                        canClick: true,
                        width: 130
                    },
                    {
                        field: 'srcTransTypeName',
                        title: '类型',
                        width: 100
                    },
                    {
                        field: 'amount',
                        title: '金额',
                        width: 90,
                        align: 'right',
                        formatter: 'formatMoney',
                        sum: true
                    },
                    {
                        field: 'leftAmount',
                        title: '余额',
                        width: 90,
                        align: 'right',
                        formatter: 'formatMoney'
                    }
                ]
            }
            if (field === 'leftAmount') {
                columns = [
                    {
                        field: 'seq',
                        type: 'seq',
                        title: '#',
                        width: 60,
                        align: 'center',
                        visible: true,
                        fixed: 'left'
                    },
                    {
                        field: 'checked',
                        type: 'checkbox',
                        title: '',
                        width: 40,
                        fixed: 'left',
                        visible: true,
                        align: 'center'
                    },
                    {
                        field: 'buName',
                        title: '往来单位',
                        width: 150
                    },
                    {
                        field: 'billDate',
                        title: '票据日期',
                        width: 100
                    },
                    {
                        field: 'srcBillNo',
                        title: '原票号',
                        canClick: true,
                        width: 130
                    },
                    {
                        field: 'srcTransTypeName',
                        title: '类型',
                        width: 100
                    },
                    {
                        field: 'amount',
                        title: '票据金额',
                        width: 90,
                        align: 'right',
                        formatter: 'formatMoney',
                        sum: true
                    },
                    {
                        field: 'hxAmount',
                        title: '已核销',
                        width: 90,
                        align: 'right',
                        formatter: 'formatMoney',
                        sum: true
                    },
                    {
                        field: 'ys',
                        title: '应收增加',
                        width: 90,
                        align: 'right',
                        formatter: 'formatMoney',
                        sum: true
                    },
                    {
                        field: 'ye',
                        title: '应收余额',
                        width: 90,
                        align: 'right',
                        formatter: 'formatMoney'
                    }
                ]
            }
            let sqls = `exec report_cus_bills_detail @tenantId=${tenantId},@buId=${id},@field='${field}',@beginDate='${beginDate}',@endDate='${endDate}' `;
            let list2 = await db.query(sqls, {type: QueryTypes.SELECT})
            ret.columns = columns;
            if (field === 'leftAmount') {
                for (let index = 0; index < list2.length; index++) {
                    if (index > 0) {
                        let it = list2[index]
                        let lastYe = list2[index - 1].ye ? tools.xe.toNumber(list2[index - 1].ye) : 0
                        let ye = it.ye ? tools.xe.toNumber(it.ye) : 0
                        list2[index].ye = tools.add(lastYe, ye)
                    }
                }
            } else {
                for (let index = 0; index < list2.length; index++) {
                    let it = list2[index]
                    if (index === 0) {
                        list2[index].leftAmount = it.amount;
                    } else {
                        list2[index].leftAmount = tools.add( list2[index - 1].leftAmount, it.amount)
                    }
                }
            }
            ret.list = list2;
            return ctx.suc('', ret);
        }
        if (page === 'reportacc') {
            if (!billDate) {
                return ctx.err('未选择时间')
            }
            beginDate = billDate.substring(0, 10)
            endDate = billDate.substring(11, 21)
            if (field === 'price1') {
                ret.columns = [
                    {
                        field: 'seq',
                        type: 'seq',
                        title: '#',
                        width: 60,
                        align: 'center',
                        visible: true,
                        fixed: 'left'
                    },
                    {
                        field: 'checked',
                        type: 'checkbox',
                        title: '',
                        width: 40,
                        fixed: 'left',
                        visible: true,
                        align: 'center'
                    },
                    {
                        field: 'billDate',
                        title: '票据日期',
                        width: 100
                    },
                    {
                        field: 'srcBillNo',
                        title: '原始票号',
                        canClick: true,
                        width: 140
                    },
                    {
                        field: 'billNo',
                        title: '凭证票号',
                        canClick: true,
                        width: 140
                    },
                    {
                        field: 'srcTransTypeName',
                        title: '原票据类型',
                        width: 110
                    },
                    {
                        field: 'amount',
                        title: '收入金额',
                        width: 130,
                        align: 'right',
                        sum: true,
                        formatter: 'formatMoney'
                    },
                    {
                        field: 'leftAmount',
                        title: '余额',
                        width: 130,
                        align: 'right',
                        formatter: 'formatMoney'
                    },
                    {
                        field: 'buName',
                        title: '往来单位',
                        width: 130,
                        align: 'left'
                    },
                    {
                        field: 'salesName',
                        title: '销售员',
                        width: 80,
                        align: 'left'
                    },
                    {
                        field: 'doName',
                        title: '经办人',
                        width: 80,
                        align: 'left'
                    },
                    {
                        field: 'remark',
                        title: '备注',
                        width: 80,
                        align: 'left'
                    }
                ]
                let list2 = await db.query(`exec report_acc_detail @tenantId=${tenantId},@subjectId=${id},@direct='in',@beginDate='${beginDate}',@endDate='${endDate}'`, {type:QueryTypes.SELECT});
                for (let i = 0; i < list2.length; i++) {
                    let it = list2[i]
                    if (i === 0) {
                        list2[i].leftAmount = it.amount;
                    }
                    else {
                        list2[i].leftAmount = tools.add(list2[i - 1].leftAmount, it.amount)
                    }
                }
                ret.list = list2;
                return ctx.suc('', ret);
            }
            if (field === 'price2') {
                ret.columns = [
                    {
                        field: 'seq',
                        type: 'seq',
                        title: '#',
                        width: 60,
                        align: 'center',
                        visible: true,
                        fixed: 'left'
                    },
                    {
                        field: 'checked',
                        type: 'checkbox',
                        title: '',
                        width: 40,
                        fixed: 'left',
                        visible: true,
                        align: 'center'
                    },
                    {
                        field: 'billDate',
                        title: '票据日期',
                        width: 120
                    },
                    {
                        field: 'billNo',
                        title: '凭证票号',
                        canClick: true,
                        width: 150
                    },
                    {
                        field: 'srcBillNo',
                        title: '原始票号',
                        canClick: true,
                        width: 150
                    },
                    {
                        field: 'srcTransTypeName',
                        title: '原票据类型',
                        width: 120
                    },
                    {
                        field: 'amount',
                        title: '支出金额',
                        width: 120,
                        align: 'right',
                        sum: true,
                        formatter: 'formatMoney'
                    },
                    {
                        field: 'leftAmount',
                        title: '余额',
                        width: 120,
                        align: 'right',
                        formatter: 'formatMoney'
                    },
                    {
                        field: 'buName',
                        title: '往来单位',
                        width: 130,
                        align: 'left'
                    },
                    {
                        field: 'salesName',
                        title: '销售员',
                        width: 80,
                        align: 'left'
                    },
                    {
                        field: 'doName',
                        title: '经办人',
                        width: 80,
                        align: 'left'
                    },
                    {
                        field: 'remark',
                        title: '备注',
                        width: 80,
                        align: 'left'
                    }
                ]
                let list2 = await db.query(`exec report_acc_detail @tenantId=${tenantId},@subjectId=${id},@direct='out',@beginDate='${beginDate}',@endDate='${endDate}'`, {type:QueryTypes.SELECT});
                for (let i = 0; i < list2.length; i++) {
                    let it = list2[i]
                    if (i === 0) {
                        list2[i].leftAmount = it.amount;
                    }
                    else {
                        list2[i].leftAmount = tools.add(list2[i - 1].leftAmount, it.amount)
                    }
                }
                ret.list = list2;
                return ctx.suc('', ret);
            }
            if (field === 'initM') {
                ret.columns = [
                    {
                        field: 'seq',
                        type: 'seq',
                        title: '#',
                        width: 60,
                        align: 'center',
                        visible: true,
                        fixed: 'left'
                    },
                    {
                        field: 'checked',
                        type: 'checkbox',
                        title: '',
                        width: 40,
                        fixed: 'left',
                        visible: true,
                        align: 'center'
                    },
                    {
                        field: 'billDate',
                        title: '票据日期',
                        width: 120
                    },
                    {
                        field: 'billNo',
                        title: '凭证票号',
                        canClick: true,
                        width: 150
                    },
                    {
                        field: 'srcBillNo',
                        title: '原始票号',
                        canClick: true,
                        width: 150
                    },
                    {
                        field: 'srcTransTypeName',
                        title: '原票据类型',
                        width: 120
                    },
                    {
                        field: 'amount',
                        title: '金额',
                        width: 120,
                        align: 'right',
                        sum: true,
                        formatter: 'formatMoney'
                    }
                ]
                let list2 = await db.query(`exec report_acc_detail @tenantId=${tenantId},@subjectId=${id},@direct='init',@beginDate='${beginDate}',@endDate='${endDate}'`, {type:QueryTypes.SELECT});
                ret.list = list2;
                return ctx.suc('', ret);
            }
            if (field === 'leftAmount') {
                ret.columns = [
                    {
                        field: 'seq',
                        type: 'seq',
                        title: '#',
                        width: 60,
                        align: 'center',
                        visible: true,
                        fixed: 'left'
                    },
                    {
                        field: 'checked',
                        type: 'checkbox',
                        title: '',
                        width: 40,
                        fixed: 'left',
                        visible: true,
                        align: 'center'
                    },
                    {
                        field: 'billDate',
                        title: '票据日期',
                        width: 120
                    },
                    {
                        field: 'billNo',
                        title: '凭证票号',
                        canClick: true,
                        width: 150
                    },
                    {
                        field: 'srcBillNo',
                        title: '原始票号',
                        canClick: true,
                        width: 150
                    },
                    {
                        field: 'srcTransTypeName',
                        title: '原票据类型',
                        width: 120
                    },
                    {
                        field: 'amount',
                        title: '金额',
                        width: 120,
                        align: 'right',
                        sum: true,
                        formatter: 'formatMoney'
                    },
                    {
                        field: 'leftAmount',
                        title: '余额',
                        width: 120,
                        align: 'right',
                        formatter: 'formatMoney'
                    },
                    {
                        field: 'buName',
                        title: '往来单位',
                        width: 120,
                        align: 'left'
                    },
                    {
                        field: 'salesName',
                        title: '销售员',
                        width: 80,
                        align: 'left'
                    },
                    {
                        field: 'doName',
                        title: '经办人',
                        width: 80,
                        align: 'left'
                    },
                    {
                        field: 'remark',
                        title: '备注',
                        width: 80,
                        align: 'left'
                    }
                ]
                let list2 = await db.query(`exec report_acc_detail @tenantId=${tenantId},@subjectId=${id},@direct='left',@beginDate='${beginDate}',@endDate='${endDate}'`, {type:QueryTypes.SELECT});
                for (let i = 0; i < list2.length; i++) {
                    let it = list2[i]
                    let amount = tools.xe.toNumber(it.amount)
                    if (i === 0) {
                        list2[i].leftAmount = amount;
                    } else {
                        let la = tools.xe.toNumber(list2[i - 1].leftAmount)
                        list2[i].leftAmount = tools.add(la, amount)
                    }
                }
                ret.list = list2;
                return ctx.suc('', ret);
            }
        }
        if (page === 'kcye') {
            let qcList = await db.query(`exec get_inventory_ye @tenantId=${tenantId},@invId=${invId}, @locationId=${locationId},@beginDate='${beginDate}',@endDate='${endDate}'`, {type:QueryTypes.SELECT});
            let list = [];
            for (let i = 0; i < qcList.length; i++) {
                let it = qcList[i]
                let one = {
                    transTypeName: '期初',
                    billNo: '',
                    billDate: '',
                    buId: 0,
                    id: 0,
                    buName: '',
                    invName: it.invName,
                    spec: it.spec,
                    unit: it.unit,
                    qty: it.initC,
                    leftQty: it.initC,
                    leftC: it.initC,
                    amount: '',
                    assQty: '',
                    price: '',
                    unit2: it.unit2,
                    unit1: it.unit1
                }
                list.push(one);
            }
            beginDate = billDate.substring(0,10)
            endDate = billDate.substring(11,21)
            let list2 = await db.query(`exec report_goods_detail @tenantId=${tenantId},@invId=${invId},@buName='${buName}',@field='${field}',@beginDate='${beginDate}',@endDate='${endDate}',@locationId=${locationId}`, {type: QueryTypes.SELECT});
            list2.map(it => {
                list.push(it)
            })
            for (let i = 0; i < list.length; i++) {
                let it = list[i]
                if (i === 0) {
                    list[i].leftQty = it.qty;
                    list[i].leftC = it.qty;
                }
                if (i > 0) {
                    let lastQty = list[i - 1].leftQty;
                    let thisQty = it.qty;
                    list[i].leftQty = tools.add(lastQty,thisQty)
                    list[i].leftC = tools.add(lastQty, thisQty)
                }
                list[i].leftC = tools.calAssQty(list[i].leftC,list[i].spec,list[i].unit1,list[i].unit2 )
            }
            ret.list = list
            return ctx.suc('', ret);
        }
        // 销售单核销的票据
        if (page === 's-ins' && field === 'hxAmount') {
            ret.columns = [
                {
                    field: 'seq',
                    type: 'seq',
                    title: '#',
                    width: 60,
                    align: 'center',
                    visible: true,
                    fixed: 'left'
                },
                {
                    field: 'checked',
                    type: 'checkbox',
                    title: '',
                    width: 40,
                    fixed: 'left',
                    visible: true,
                    align: 'center'
                },
                {
                    field: 'transTypeName',
                    title: '类型',
                    width: 100
                },
                {
                    field: 'buName',
                    title: '往来单位',
                    width: 150
                },
                {
                    field: 'billDate',
                    title: '票据日期',
                    width: 100
                },
                {
                    field: 'billNo',
                    title: '票号',
                    canClick: true,
                    width: 130
                },

                {
                    field: 'amount',
                    title: '票据金额',
                    width: 90,
                    align: 'right',
                    formatter: 'formatMoney',
                    sum: true
                },
                {
                    field: 'createdAt',
                    title: '创建时间',
                    width: 120,
                    align: 'center',
                }
            ]
            let sqls = `SELECT a.*,a.transType srcTransType,a.billNo srcBillNo from v_invoice a INNER JOIN invoice_receipt b on a.id=b.iid where b.billId=${id}`
            ret.list = await db.query(sqls,{type: QueryTypes.SELECT})
            return ctx.suc('', ret);
        }
    } catch (e) {
        return err(e);
    }
})
module.exports = router;